using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLYT.Application.Interfaces;
using WLYT.Domain.ApiModel;
using WLYT.Domain.Model;
using WLYT.Repository;

namespace WLYT.Application.Services
{	
    public class SupplierService : BaseService<Supplier>, ISupplierService
    {
        public ISqlSugarRepository<Supplier> SupplierRepository;
        public ISqlSugarRepository<Order> OrderRepository;
        public SupplierService(ISqlSugarRepository<Supplier> supplierRepository,
            ISqlSugarRepository<Order> orderRepository) : base(supplierRepository)
        {
            SupplierRepository = supplierRepository;
            OrderRepository = orderRepository;
        }

        public async Task<Tuple<IList<Supplier>, int>> GetModelsByPageAsync(string itemCode, string itemName, int pageSize, int pageIndex)
        {
            Expressionable<Supplier> exp = Expressionable.Create<Supplier>();
            exp.AndIF(!string.IsNullOrEmpty(itemCode), p => p.SupplierCode.Contains(itemCode));
            exp.AndIF(!string.IsNullOrEmpty(itemName), p => p.SupplierName.Contains(itemName));
            return await GetPageListAsync(exp, pageSize, pageIndex, p => p.Id);
        }

        public async Task<ApiResult> SyncSupplier(List<SupplierModel> list)
        {
            ApiResult result = new ApiResult();
            try
            {
                await _baseRepository.AsTenant().BeginTranAsync();
                foreach (var item in list)
                {
                    var model = await SupplierRepository.GetFirstAsync(p => p.SupplierCode == item.supplier_code);
                    if (model == null)
                    {
                        await SupplierRepository.InsertAsync(new Supplier
                        {
                            SupplierCode = item.supplier_code,
                            CreateTime = DateTime.Now,
                            SupplierName = item.supplier_name ?? ""
                        });
                    }
                    else
                    {
                        model.UpdateTime = DateTime.Now;
                        model.SupplierCode = item.supplier_code;
                        model.SupplierName = item.supplier_name ?? "";
                        await SupplierRepository.UpdateAsync(model);
                    }
                    if (!string.IsNullOrEmpty(item.supplier_name))
                    {
                        await OrderRepository.UpdateAsync(p => new Order { SupplierName = item.supplier_name }, p => p.SupplierCode == item.supplier_code && p.SupplierName == "");
                    }
                }
                await _baseRepository.AsTenant().CommitTranAsync();
            }
            catch (Exception ex)
            {
                await _baseRepository.AsTenant().RollbackTranAsync();
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}